<?php
include("chk_permission.php");//must to be member
//check permission -------------------------
if($user_type <> "2" && $user_type <> "3"  && $user_type <> "4"  && $user_type <> "5"){// admin and director only
	?>
		<script type="text/javascript">
			window.location.href='index.php';
		</script>
	<?
		exit();
}
// -----------------------------------------
include("connect/connect_dsccmis.php");
//include "common/commonFns.php";
$Submit=$_POST["Submit"];
$pid=$_POST["pid"];
$nme=$_POST["nme"];
$evndte=$_POST["evndte"];

?>
<html>
<head>
<title>:: ตัวอย่าง ทำ AJAX ที่ไว้เลือกจังหวัด อำเภอ ตำบล ของไทย ::</title>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<script src="common/calendar_selector.js" type="text/javascript"></script>
<script src="common/jsfunction.js" type="text/javascript"></script>
<script language=Javascript>
function Inint_AJAX() {
   try { return new ActiveXObject("Msxml2.XMLHTTP");  } catch(e) {} //IE
   try { return new ActiveXObject("Microsoft.XMLHTTP"); } catch(e) {} //IE
   try { return new XMLHttpRequest();          } catch(e) {} //Native Javascript
   alert("XMLHttpRequest not supported");
   return null;
};

function dochange(src, val) {
     var req = Inint_AJAX();
     req.onreadystatechange = function () { 
          if (req.readyState==4) {
               if (req.status==200) {
                    document.getElementById(src).innerHTML=req.responseText; //รับค่ากลับมา
               } 
          }
     };
     req.open("GET", "get_catm.php?data="+src+"&val="+val); //สร้าง connection
     req.setRequestHeader("Content-Type", "application/x-www-form-urlencoded;charset=tis-620"); // set Header
     req.send(null); //ส่งค่า
}


//window.onLoad=dochange('province', -1);     
</script>
</head>
<body>

<br>
<form name="sel" method="post" action="index.php?tab=vsearch">
  <table border="0" cellspacing="0">
    <tr>
      <td height="30">เลขประจำตัวประชาชน</td>
      <td height="30"><input name="pid" type="text" id="pid" size="13" maxlength="13" value="<?=$pid?>"></td>
      <td height="30">ชื่อ/สกุล</td>
      <td height="30"><input name="nme" type="text" id="nme" size="20" value="<?=$nme?>"></td>
      <td height="30">วันที่ประสบเหตุ</td>
      <td height="30"><input name="evndte" type="text" id="evndte" size="10" maxlength="10" value="<?=$evndte?>">
      <a href="#" onClick="return getCalendar(document.forms[0].evndte);"><img src="images/calendar.png" border="0" /></a></td>
    </tr>
    <tr>
      <td height="30">จังหวัด (ที่เกิดเหตุ):</td>
      <td height="30" colspan="5"><font id=province>
        <select name="select">
          <option value='0'>============</option>
        </select>
      </font> อำเภอ : <font id=amper>
      <select name="select">
        <option value='0'>==== ไม่มี ====</option>
      </select>
      </font> ตำบล : <font id=tumbon>
      <select name="select">
        <option value='0'>==== ไม่มี ====</option>
      </select>
      </font></td>
    </tr>
    <tr>
      <td height="30" colspan="6"><div align="center">
        <input type="submit" name="Submit" value="ค้นหา">
        <input name="reset" type="reset" value="ยกเลิก">
      </div></td>
    </tr>
  </table>
<br>
<?php
//if($_POST['province']<>"0"){
	echo "<script>dochange('province','".$_POST['province']."');</script>";
//}
if($_POST['amper']<>"0"){
	echo "<script>dochange('amper','".$_POST['amper']."');</script>";
}else{
	echo "<script>dochange('amper','".$_POST['province']."');</script>";
}
if($_POST['tumbon']<>"0"){
	echo "<script>dochange('tumbon','".$_POST['tumbon']."');</script>";
}else{
	echo "<script>dochange('tumbon','".$_POST['amper']."');</script>";
}

?>
</form>
<?php
if($Submit<>""){
    if($_POST['tumbon']<>"0"){
		$cond_plc=" and evnplcid like '".$_POST['tumbon']."%'";
	}else{
		if($_POST['amper']<>"0"){
			$cond_plc=" and evnplcid like '".$_POST['amper']."%'";
		}else{
	 		if($_POST['province']<>"0"){
				$cond_plc=" and evnplcid like '".$_POST['province']."%'";
			}else{
				$cond_plc="";
			}
		}
	}
	//เงื่อนไขเลขประจำตัวประชาชน
	$cond_pid="";
	if($pid<>""){
		$cond_pid=" and persiden='$pid'";
	}
	//เงื่อไขชื่อ
	$cond_nme="";
	if($nme<>""){
		$cond_nme=" and (nme like '%$nme%' or surnme like '%$nme%')";
	}
	$evndte = str_replace('-','',$evndte);
	mysql_query("set names utf8");
	$sql="select evnid,perid,persiden,nme,surnme,evndte,evnnme,roladr,rolvllno,rolplcnme,plcnme,losbodynme from mis_tempdsccformcc where evndte1 like '$evndte%'".$cond_plc.$cond_pid.$cond_nme;
	//echo $sql;
	$rs=mysql_query($sql);
?>
<table width="800" border="1" cellspacing="0">
<?php
	if($row=mysql_fetch_array($rs)){
?>
  <tr>
    	<td><div align="center"><strong>เลขประจำตัวประชาชน</strong></div></td>
   	<td><div align="center"><strong>ชื่อ-สกุล</strong></div></td>
    	<td width="90"><div align="center"><strong>ประสบเหตุเมื่อ</strong></div></td>
    	<!--td><div align="center"><strong>พื้นที่ประสบเหตุ</strong></div></td-->
    	<td><div align="center"><strong>การบาดเจ็บ</strong></div></td>
        <td><div align="center"><strong>ที่อยู่ตามทะเบียนบ้าน</strong></div></td>
  </tr>
<?php
		do{
?>
  	<tr>
    	<td><div align="left">
   	    <?=$row["persiden"]?>
  	    </div></td>
    	<td><a href="#" onClick="popUp('show_victim_des.php?perid=<?=$row["perid"]?>','400','400');"><?=$row["nme"]." ".$row["surnme"]?></a></td>
    	<td><div align="center">
   	    <?=$row["evndte"]?>
  	  </div></td>
    	<td><?=$row["losbodynme"]?></td>
  	    <td><?=$row["roladr"]." หมู่ ".$row["rolvllno"]." ".$row["rolplcnme"]?></td>
  	</tr>
<?php
		}while($row=mysql_fetch_array($rs));
	}
?>
</table>
<?php
}
?>
</body>

</html>